﻿using System;
using System.Collections.Generic;

using System.Linq;
using System.Text;
using System.Web;

using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration;
using System.Web.UI.WebControls;
using System.Data;
using System.Reflection;
using zevenseas.LifeCycleManagement.Utilities;
using System.Web.UI;

namespace zevenseas.LifeCycleManagement.ApplicationPages
{
    public class OverviewDeletedSites : Microsoft.SharePoint.WebControls.LayoutsPageBase
    {      
        protected InputFormTextBox TxtUrlSearch;        
        protected WebApplicationSelector Selector;
        protected SPGridView gridView;
              
        private ObjectDataSource dataSource;
        private DataView dataView;      

        protected override void CreateChildControls()
        {
            gridView.ID = "gridView";
            gridView.AutoGenerateColumns = false;
            gridView.AlternatingRowStyle.BackColor = System.Drawing.Color.WhiteSmoke;

            // Set EventHandler for setting the filter icon
            gridView.RowDataBound += new GridViewRowEventHandler(m_oGridView_RowDataBound);

            // gridView.Sorting += new GridViewSortEventHandler(gridView_Sorting);

            // Create the datasource object
            dataSource = new ObjectDataSource();

            string strTypeName = "zevenseas.LifeCycleManagement.ApplicationPages.OverviewDeletedSites,";
            strTypeName += Assembly.GetExecutingAssembly().FullName;

            dataSource.TypeName = strTypeName;
            dataSource.SelectMethod = "GetSites";
            dataSource.ID = "UnusedSitesDataSource";

            dataView = (DataView)dataSource.Select();

            if (TxtUrlSearch != null)
            {
                if (!string.IsNullOrEmpty(TxtUrlSearch.Text.ToString()))
                {
                    dataView.RowFilter = "Url LIKE '*" + this.TxtUrlSearch.Text + "*'";
                }
            }


            HttpRequest oRequest = HttpContext.Current.Request;
            if ((oRequest.Form["__CALLBACKID"] == null) ||
                (oRequest.Form["__CALLBACKPARAM"] == null) ||
                (!oRequest.Form["__CALLBACKID"].EndsWith("gridView")))
            {
                if (null != ViewState["FilterExpression"])
                {
                    dataSource.FilterExpression = (string)ViewState["FilterExpression"];
                }
            }

            Controls.Add(dataSource);

            // Bind SPGridView to datasource object            
            gridView.DataSource = dataView;

            //gridView.PagerTemplate = null;

             


            base.CreateChildControls();
        }

        public void SearchLinkClick(object sender, EventArgs e)
        { 
        
        }


        protected override void OnPreRender(System.EventArgs e)
        {

            ViewState["FilterExpression"] = dataSource.FilterExpression;
            base.OnPreRender(e);

        }

        // --------------------

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            //gridView.DataBind();

            BindData();
            base.Render(writer);
        }

        // --------------------

        void m_oGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((null != sender) && (e.Row.RowType == DataControlRowType.Header))
            {
                string strFilteredColumn = ((SPGridView)sender).FilterFieldName;
                SetGridViewFilterIcon(gridView, strFilteredColumn, e.Row);
            }
        }

        // --------------------

        public void SetGridViewFilterIcon(SPGridView gridView, string strColumn, GridViewRow row)
        {
            if ((false == string.IsNullOrEmpty(strColumn)) && (null != row))
            {
                // Show icon on filtered column
                for (int iIndex = 0; iIndex < gridView.Columns.Count; iIndex++)
                {
                    DataControlField oField = gridView.Columns[iIndex];

                    if (oField.HeaderText == strColumn)
                    {
                        Image filterIcon = new Image();
                        filterIcon.ImageUrl = "/_layouts/images/ewr093.gif";
                        filterIcon.ImageAlign = ImageAlign.Left;
                        filterIcon.Style[System.Web.UI.HtmlTextWriterStyle.MarginTop] = "2px";
                        filterIcon.ID = "FilterIcon";

                        Panel panel = new Panel();
                        panel.Controls.Add(filterIcon);

                        row.Cells[iIndex].Controls.Add(panel);

                        break;
                    }
                }
            }
        }

        public void BindData()
        {
            try
            {
                dataView = (DataView)dataSource.Select();

                //if (Selector != null)
                //{
                //    dataView.RowFilter = "WebApplication LIKE '*" + Selector.CurrentItem.Name + "*'";
                //}

                if (TxtUrlSearch != null)
                {
                    if (!string.IsNullOrEmpty(TxtUrlSearch.Text.ToString()))
                    {
                        dataView.RowFilter = "Link LIKE '*" + TxtUrlSearch.Text + "*'";
                    }
                }

                gridView.DataSource = dataView;
                gridView.DataBind();
            }

            catch (Exception databindError)
            {

            }

        }


        public DataTable GetSites()
        {
            //if (Utilities.Utilities.CheckIfLCMExists())
            //{
            //    SPList deletedSitesList = SPContext.Current.Web.Webs["LCM"].Lists["DeletedSites"];
            //    SPQuery query = new SPQuery();
            //    query.Query = "<OrderBy><FieldRef Name='ID' Ascending='True' /></OrderBy>";
            //    sitesDataTable = deletedSitesList.GetItems(query).GetDataTable();
            //}

            DataTable sitesDataTable = new DataTable();
            sitesDataTable.Columns.Add("Title");
            sitesDataTable.Columns.Add("Url");
            sitesDataTable.Columns.Add("FileName");
            sitesDataTable.Columns.Add("FileLocation");
            sitesDataTable.Columns.Add("SiteCollection");
            sitesDataTable.Columns.Add("Web");
            sitesDataTable.Columns.Add("Date");

            DeletedSiteCollection collection = new DeletedSiteCollection(Selector.CurrentItem);

            foreach (DeletedSite deletedSite in collection)
            {
                DataRow row = sitesDataTable.NewRow();
                row["Title"] = deletedSite.Id;
                row["Url"] = deletedSite.Name;
                row["FileName"] = deletedSite.FileName.ToString();
                row["FileLocation"] = deletedSite.FileLocation.ToString();
                row["SiteCollection"] = deletedSite.SiteCollection;
                row["Web"] = deletedSite.Web;
                row["Date"] = deletedSite.Date;
                sitesDataTable.Rows.Add(row);
            }
           

            return sitesDataTable;
        }

        

        
        
    }
}
